# coding=utf-8

import re


def get_time(week, tmp_file):
    time = []
    for line in tmp_file:
        tmp_line = line.split()
        lens = len(tmp_line)
        # print(lens)
        if lens > 3:
            if tmp_line[0] in week[:]:
                time.append(line.strip())
    return time


def get_logs(time, filetxt):
    i = 0
    n = i + 1
    while i < len(time) - 1:
        while time[i] == time[n]:
            n += 1
            if n == len(time) - 1:
                break
        pattern = re.compile(time[i] + '(.*?)' + time[n], re.S)
        result = re.findall(pattern, filetxt)
        seltxt = result[0]
        if "ORA-" in seltxt or "Errors" in seltxt or "error" in seltxt or "TNS-" in seltxt:
            print('\n\n===>>报错起始时间:', time[i])
            print(''.join(result))
            # print(type(result))
            print('<<===报错终止始时间:', time[n])
        i = n


def main():
    logfile = r'C:\Users\admin\Desktop\listener.log'
    week = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    file = open(logfile)
    filelines = file.readlines()
    tmp_file = filelines[-200:]
    time = get_time(week, tmp_file)
    filetxt = "".join(tmp_file).strip()
    get_logs(time, filetxt)


if __name__ == '__main__':
    main()
